package ua.mk.chdu.rtem.diploma.core.agents.scheduling.behaviours;

import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import ua.mk.chdu.rtem.diploma.core.agents.scheduling.TaskSchedulingAgent;
import ua.mk.chdu.rtem.diploma.core.model.enums.MessageTypes;
import ua.mk.chdu.rtem.diploma.core.model.vo.PerformanceReportVO;
import ua.mk.chdu.rtem.diploma.core.utils.MessageContentExtractor;

public class ReceivePerformanceReportBehaviour extends OneShotBehaviour {
    private TaskSchedulingAgent agent;
    private MessageTemplate template = MessageTemplate.MatchInReplyTo(MessageTypes.GET_PERFORMANCE_REPORT.getValue());
    private MessageContentExtractor<PerformanceReportVO> extractor = new MessageContentExtractor<PerformanceReportVO>();

    public ReceivePerformanceReportBehaviour(TaskSchedulingAgent agent) {
        this.agent = agent;
    }

    @Override
    public void action() {
        System.out.println("STARTED RECEIVING PERFORMANCE REQUEST");
        ACLMessage reportResult = agent.receive(template);
        if (reportResult != null) {
            System.out.println("PERFORMANCE REPORT RECEIVED");
            PerformanceReportVO performanceReportVO = extractor.extract(reportResult);
            agent.getModel().setPerformanceReport(performanceReportVO);
        } else {
            System.out.println("PERFORMANCE REPORT RECEIVING EVENT BLOCKED");
            block();
        }
    }
}
